package com.example.neusoft215gdh.androidpro_21.Model;
import com.example.neusoft215gdh.androidpro_21.Interface.LoginAndRegisterFace;
import com.example.neusoft215gdh.androidpro_21.JavaBean.StringBean;
import com.example.neusoft215gdh.androidpro_21.Listener.UpdateListener;
import com.example.neusoft215gdh.androidpro_21.Listener.UserListener;
import com.example.neusoft215gdh.androidpro_21.Service.LoginService;
import com.example.neusoft215gdh.androidpro_21.Service.MySelfService;
import com.example.neusoft215gdh.androidpro_21.Service.RegisterService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**UserModel业务逻辑处理
 * Created by hp-pc on 2017/9/26.
 */
public class UserModel implements LoginAndRegisterFace{

   private RegisterService  registerService;
    private LoginService loginService;
    private MySelfService mySelfService;
    private Retrofit retrofit;
    private static final String BASE_URL="http://172.24.10.175/workout/api.php/";//主机地址

    public UserModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())//解析方式
                .build();
    }

    /**
     * 用户登录模块
     * 高德翰
     * @param tel 电话号码
     * @param password 密码
     * @param userListener
     */
    @Override
    public void getLogin(String tel, String password, final  UserListener userListener) {
     loginService=retrofit.create(LoginService.class);
        Call<StringBean> call=loginService.LoginImpl(tel,password);

        call.enqueue(new Callback<StringBean>() {
            @Override
            public void onResponse(Call<StringBean> call, Response<StringBean> response) {
                if (response.isSuccessful()&&response.body().getError()==null)
                    userListener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<StringBean> call, Throwable t) {
                     userListener.onFail("登录失败!");
            }
        });
    }

    /**
     * 用户注册模块

     * @param tel 电话号码
     * @param password 密码
     * @param userListener
     */
    @Override
    public void getRegister(String tel, String password, final UserListener userListener) {
        registerService=retrofit.create(RegisterService.class);
        Call<StringBean> call=registerService.RegImpl(tel,password);
        call.enqueue(new Callback<StringBean>() {
            @Override
            public void onResponse(Call<StringBean> call, Response<StringBean> response) {
                if (response.isSuccessful()&&response.body().getError()==null) {
                    userListener.onResponse(response.body());
                }
            }
            @Override
            public void onFailure(Call<StringBean> call, Throwable t) {
            userListener.onFail("注册失败!");
            }
        });
    }


//  更改密码
    @Override
    public void updatePsd(String sessionid, String userid, String password, final  UpdateListener updateListener) {
        mySelfService=retrofit.create(MySelfService.class);
        Call<Integer> call=mySelfService.update(sessionid,userid,password);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.isSuccessful()&&response.body()!=null) {
                       updateListener.onResponseUp(response.body());
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
    }


}
